<?php

namespace frontend\modules\business\controllers;

use yii;
use yii\web\Controller;
use yii\helpers\Url;

/**
 * Default controller for the `business` module
 */
class DefaultController extends Controller
{
    public $business = [];
    
    public $master = null;
    
    public function init()
    {
        parent::init();
        
        $this->enableCsrfValidation = false;
        
        $this->business = $this->module->params['business'];
        
        $masterConfig = $this->module->params['master'];
        
        $this->master = new yii\db\Connection([
            'dsn' => "mysql:host={$masterConfig['host']};dbname={$masterConfig['dbname']}",
            'username' => $masterConfig['username'],
            'password' => $masterConfig['password'],
            'charset' => $masterConfig['charset'],
            'tablePrefix' => $masterConfig['tablePrefix'],
        ]);
        
    }

    public function actionIndex()
    {
        return $this->render('index',['business' => $this->business]);
    }
    
    public function actionShow()
    {
        $admin_username = '';
        
        if (Yii::$app->request->isPost) {
            $admin_id = Yii::$app->request->post('admin_id');
            $admin_username = Yii::$app->request->post('admin_username');
        } else {
            $admin_id = Yii::$app->request->get('admin_id');
        }

        if (intval($admin_id) == 0 && $admin_username == '') {
            $this->redirect(['index']);
        }
        $where = [];
        
        $where['a.id'] = $admin_id;
        if ($admin_username) {
            $where['a.username'] = $admin_username;
        }

        $business = (new \yii\db\Query())
        ->select(['a.id','a.username','b.is_alone','a.weichat_id'])
        ->from('busy_admin_user as a')
        ->leftJoin(['b' => 'busy_admin_user_base'], 'b.id = a.id')
        ->where($where)
        ->all($this->master);

        return $this->render('show', ['business'=>$business]);
    }

}
